package com.pro.service.impl;

import com.pro.VO.adminVO.CatVo;
import com.pro.bean.CourseCategory;
import com.pro.dao.CourseCategoryDao;
import com.pro.service.CourseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * CourseCategoryService接口实现类
 * */
@Service
public class CourseCategoryServiceImpl implements CourseCategoryService {
    //将dao接口注入到属性中
    @Autowired
    private CourseCategoryDao courseCategoryDao;

    /***
     * 查询课程分类表中所有分类
     */
    public List<CatVo> listCategory(){
        //查询一级分类
        List<CourseCategory> l1CatList = courseCategoryDao.selLevel1();
        List<CatVo> categoryList = new ArrayList<>(l1CatList.size());
        for (CourseCategory l1 : l1CatList) {//遍历一级分类
            CatVo l1CatVo = new CatVo();
            l1CatVo.setValue(l1.getId());//将一级分类id传入视图对象CatVO中value列
            l1CatVo.setLabel(l1.getCategoryname());//将一级分类名传入视图对象CateVO中Label列

            //查询二级分类
            List<CourseCategory> l2CatList = courseCategoryDao.selLevel2(l1.getId());
            List<CatVo> children1 = new ArrayList<>(l2CatList.size());
            for (CourseCategory l2 : l2CatList){//遍历二级分类
                CatVo l2CatVo = new CatVo();
                l2CatVo.setValue(l2.getId());//将二级分类id传入视图对象CatVO中value列
                l2CatVo.setLabel(l2.getCategoryname());//将二级分类名传入视图对象CateVO中Label列

                List<CourseCategory> l3CatList = courseCategoryDao.selCategoryLevel3(l2.getId()); //遍历出三级分类
                List<CatVo> children2 = new ArrayList<>(l3CatList.size());
                for(CourseCategory l3 : l3CatList){
                    CatVo l3CatVo = new CatVo();
                    l3CatVo.setValue(l3.getId());
                    l3CatVo.setLabel(l3.getCategoryname());
                    children2.add(l3CatVo);
                }
                l2CatVo.setChildren(children2); //将children2（三级分类）放入二级分类的l2CatVO（子分类）中
                children1.add(l2CatVo);//将当前遍历取出的二级分类三级分类放入children1中
            }
            l1CatVo.setChildren(children1);//将children1（二级三级分类）放入一级分类的l1CatVO（子分类）中

            categoryList.add(l1CatVo); //每循环一次一级分类后将该视图对象传入categoryList中
        }
            return categoryList;
    }

    /**
     *
     * 查询表中二级分类的相关信息
     *
     * */
    public List<CourseCategory> selCategoryLevel2() {
        return courseCategoryDao.selCategoryLevel2();
    }

    /**
     * 根据传参过来的二级分类id找到对应三级分类相关信息
     * */
    public List<CourseCategory> selCategoryLevel3(int parentId) {
        return courseCategoryDao.selCategoryLevel3(parentId);
    }
}
